package com.star.img;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.lang.GeoLocation;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.GpsDirectory;

import java.io.File;
import java.io.IOException;
import java.util.Objects;


/**
 * @author: liminghui
 * @date: 2021/6/1 17:13
 * @version: 1.0
 * @description: 根据照片原图解析经纬度
 */
public class PictureTest {


    public static void main(String[] args) throws JpegProcessingException, IOException {
        readPic2("data/2.jpg");
    }

    private static void readPic2(String pathname) throws JpegProcessingException, IOException {
        File jpegFile = new File(pathname);
        Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
        GpsDirectory gpsDirectory = metadata.getDirectory(GpsDirectory.class);
        if (Objects.nonNull(gpsDirectory)) {
            GeoLocation geoLocation = gpsDirectory.getGeoLocation();
            System.out.println(geoLocation.getLongitude());
            System.out.println(geoLocation.getLatitude());
        }
    }

    public static void readPic(String pathname) throws JpegProcessingException, IOException {
        File jpegFile = new File(pathname);
        Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
        //获取图片所有EXIF信息
        Iterable<Directory> directories = metadata.getDirectories();
        for (Directory directory : directories) {
            for (Tag tag : directory.getTags()) {
                System.out.println(tag);
            }
        }
    }

}
